package com.rosense.client;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;

import com.rosense.client.rpc.RPCBean;
import com.rosense.client.rpc.RPCCpuBean;

/**
 * 采集CPU使用率 
 * @author 李岩飞
 * @email eliyanfei@126.com	
 * 2018年1月23日 下午1:44:59
 * 
 */
public class CpuUsage extends ResourceUsage {
	private static CpuUsage INSTANCE = new CpuUsage();

	private CpuUsage() {
	}

	public static CpuUsage getInstance() {
		return INSTANCE;
	}

	/**
	 * 功能：CPU使用信息
	 * */
	public static Map<?, ?> cpuinfo() {
		InputStreamReader inputs = null;
		BufferedReader buffer = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			inputs = new InputStreamReader(new FileInputStream("/proc/stat"));
			buffer = new BufferedReader(inputs);
			String line = "";
			boolean readOne = true;
			int cores = 0;
			while (true) {
				line = buffer.readLine();
				if (line == null) {
					break;
				}
				if (line.startsWith("cpu")) {
					if (readOne) {
						StringTokenizer tokenizer = new StringTokenizer(line);
						List<String> temp = new ArrayList<String>();
						while (tokenizer.hasMoreElements()) {
							String value = tokenizer.nextToken();
							temp.add(value);
						}
						map.put("user", temp.get(1));
						map.put("nice", temp.get(2));
						map.put("system", temp.get(3));
						map.put("idle", temp.get(4));
						map.put("iowait", temp.get(5));
						map.put("irq", temp.get(6));
						map.put("softirq", temp.get(7));
						map.put("stealstolen", temp.get(8));
					}
					cores++;
				}
			}
			map.put("cores", cores);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				buffer.close();
				inputs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return map;
	}

	/** 
	 * Purpose:采集CPU使用率 
	 * @param args 
	 * @return float,CPU使用率,小于1 
	 */
	@Override
	public RPCBean scan() {
		RPCCpuBean rpc = new RPCCpuBean();
		try {
			Map<?, ?> map1 = cpuinfo();
			TimeUnit.SECONDS.sleep(3);
			Map<?, ?> map2 = cpuinfo();

			long user1 = Long.parseLong(map1.get("user").toString());
			long nice1 = Long.parseLong(map1.get("nice").toString());
			long system1 = Long.parseLong(map1.get("system").toString());
			long idle1 = Long.parseLong(map1.get("idle").toString());

			long user2 = Long.parseLong(map2.get("user").toString());
			long nice2 = Long.parseLong(map2.get("nice").toString());
			long system2 = Long.parseLong(map2.get("system").toString());
			long idle2 = Long.parseLong(map2.get("idle").toString());

			long total1 = user1 + system1 + nice1;
			long total2 = user2 + system2 + nice2;
			float total = total2 - total1;

			long totalIdle1 = user1 + nice1 + system1 + idle1;
			long totalIdle2 = user2 + nice2 + system2 + idle2;
			float totalidle = totalIdle2 - totalIdle1;

			float cpusage = (total / totalidle);
			rpc.cpusage =cpusage;
			rpc.cores = Integer.parseInt(map2.get("cores").toString())-1;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return rpc;
	}
}
